package cn.edu.lcu.cs.javaprogramming.gui;

import cn.edu.lcu.cs.javaprogramming.annotations.JTableColumnProcessor;
import cn.edu.lcu.cs.javaprogramming.collection.Score;
import cn.edu.lcu.cs.javaprogramming.collection.ScoreUtil;
import cn.edu.lcu.cs.javaprogramming.collection.UserUtil;
import cn.edu.lcu.cs.javaprogramming.oop.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.util.List;
import java.util.Set;

public class TableDlg extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JButton scoreBtn;
    private JButton userBtn;
    private JTable table;

    public TableDlg() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // 点击 X 时调用 onCancel()
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 遇到 ESCAPE 时调用 onCancel()
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        scoreBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Set<Score> scores = ScoreUtil.getScores();
                Object[] columnNames = JTableColumnProcessor.getColumnNames(Score.class);
                Object[][] rows = JTableColumnProcessor.getRows(scores);
                DefaultTableModel defaultTableModel = new DefaultTableModel(rows, columnNames);
                table.setModel(defaultTableModel);
            }
        });
        userBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<User> users = UserUtil.getUsers();
                Object[] columnNames = JTableColumnProcessor.getColumnNames(User.class);
                Object[][] rows = JTableColumnProcessor.getRows(users);
                DefaultTableModel defaultTableModel = new DefaultTableModel(rows, columnNames);
                table.setModel(defaultTableModel);
            }
        });
    }

    private void onOK() {
        // 在此处添加您的代码
        dispose();
    }

    private void onCancel() {
        // 必要时在此处添加您的代码
        dispose();
    }

    public static void main(String[] args) {
        TableDlg dialog = new TableDlg();
        dialog.setTitle("JTable表格演示");
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}
